<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Api\Controller;
use App\Models\Topic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use zgldh\QiniuStorage\QiniuStorage;

class TopicController extends Controller
{

  //列表
  public function index(Request $request)
  {
    $papers = Topic::getList($request);
    return $this->success($papers);
  }

  // 详情
  public function detail($id)
  {
    $paperInfo = Topic::getDetail($id);
    return $this->success($paperInfo);
  }

  // 新增
  public function add(Request $request)
  {
    $data = $request->all();
    $rule = [
      // 'title'     => 'required',
      'cateid'    => 'required',
    ];
    $message = [
      // 'title.required'       => '标题不能为空',
      'cateid.required'      => '分类不能为空',
    ];
    $validated = $this->validator($data, $rule, $message);
    if (is_bool($validated)) {
      $insertData = [
        'title' => $request->title ?? '',
        'en_title' => $request->en_title ?? '',
        'img' => $request->img ?? '',
        'description' => $request->description ?? '',
        'url' => $request->url ?? '',
        'content' => $request->content ?? '',
        'en_content' => $request->en_content ?? '',
        'm_content' => $request->m_content ?? '',
        'm_en_content' => $request->m_en_content ?? '',
        'cateid' => $request->cateid,
        'cateids' => $request->cateids && count($request->cateids)>0 ? implode(',', $request->cateids):'', 
        'catename' => $request->catename,
        'status' => $request->status,
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' => date('Y-m-d H:i:s'),
      ];
      // print_r($insertData);exit;
      $result = DB::table('topics')->insert($insertData);
      return $this->success();
    } else {
      return $validated;
    }
  }

  // 修改
  public function update(Request $request)
  {
    $paperInfo = DB::table('topics')->where('id', $request->id)->first();
    if ($paperInfo && $paperInfo->id) {
      if(isset($request->type) && $request->type === 'operateStatus') {
        $updateData = [
          'status' => $request->status,
          'updated_at' => date('Y-m-d H:i:s'),
        ];
      } else {
        $updateData = [
          'title' => $request->title ?? '',
          'en_title' => $request->en_title ?? '',
          'img' => $request->img ?? '',
          'description' => $request->description ?? '',
          'url' => $request->url ?? '',
          'content' => $request->content ?? '',
          'en_content' => $request->en_content ?? '',
          'm_content' => $request->m_content ?? '',
          'm_en_content' => $request->m_en_content ?? '',
          'cateid' => $request->cateid ?? $paperInfo->cateid,
          'catename' => $request->catename ?? $paperInfo->catename,
          'status' => $request->status ?? $paperInfo->status,
          'updated_at' => date('Y-m-d H:i:s'),
        ];
      }
      $result = DB::table('topics')->where('id', $request->id)->update($updateData);
      if ($result) {
        return $this->success();
      }
      return $this->failed('修改失败');
    } else {
      return $this->failed('内容不存在');
    }
  }

  // 删除
  public function delete($id)
  {
    $result = Topic::del($id);
    if ($result) {
      return $this->success();
    }
    return $this->failed('删除失败');
  }
}
